<?php

namespace App\Repository;
use App\Models\Base\Resource;
use App\Models\Role\RoleMenuBind;
use App\Models\Role\RoleResourceBind;

/**
 * 系统资源数据仓储类
 *
 * @package App\Http\Repository
 * @author WangBin
 * @mail: <849850352@qq.com>
 */
class ResourceRepository extends BaseRepository
{
    /**
     * 获取资源列表
     *
     * @param array $filter
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function getList(array $filter = [])
    {
        $query = Resource::query()->orderByDesc('sort_num')
            ->select(['base_resource.*', 'base_menu.menu_name'])
            ->leftJoin('base_menu', 'base_resource.menu_id', '=', 'base_menu.menu_id');
        if(!empty($filter['resource_name'])) {
            $query->where('resource_name', 'like', '%' . $filter['resource_name'] . '%');
        }
        if(isset($filter['is_active'])) {
            $query->where('base_resource.is_active', $filter['is_active']);
        }
        if(!empty($filter['menu_id'])) {
            $query->where('base_resource.menu_id', $filter['menu_id']);
        }
        return $query;
    }

    /**
     * 获取角色绑定的资源
     *
     * @param int $roleId
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function getBindList(int $roleId)
    {
        return RoleResourceBind::query()->where('role_id', $roleId);
    }

    /**
     * 删除角色绑定的资源
     *
     * @param int $roleId
     * @return mixed
     */
    public function deleteByRoleId(int $roleId)
    {
        return RoleResourceBind::query()->where('role_id', $roleId)->delete();
    }

    /**
     * 插入角色绑定
     *
     * @param array $ids
     * @param int $roleId
     */
    public function updateResource(array $ids, int $roleId)
    {
        foreach ($ids as $id) {
            RoleResourceBind::query()->insert(['resource_id' => $id, 'role_id' => $roleId]);
        }
    }

    /**
     * 保存资源
     *
     * @param array $data
     * @param int $resourceId
     * @return int|mixed
     */
    public function save(array $data, int $resourceId = 0)
    {
        $query = $resourceId ? Resource::find($resourceId) : new Resource();
        foreach ($data as $field => $val) {
            $query->{$field} = $val;
        }
        return $query->save() ? $query->resource_id : 0;
    }

    /**
     * 根据 Id 获取资源
     *
     * @param int $resourceId
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getById(int $resourceId)
    {
        return Resource::query()->where('resource_id', $resourceId)->first();
    }

    /**
     * 获取二级菜单可用的资源
     *
     * @param int $menuId
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getByMenuId(int $menuId)
    {
        return Resource::query()->where('is_active', 1)->where('menu_id', $menuId)->get();
    }

    /**
     * 删除角色绑定的菜单
     *
     * @param int $roleId
     * @return mixed
     */
    public function deleteMenuByRoleId(int $roleId)
    {
        return RoleMenuBind::query()->where('role_id', $roleId)->delete();
    }
}
